package com.leetCode.ListNodeDemo;

import javax.swing.*;
import java.util.List;

public class ReverseListDemo {
    public static void main(String[] args) {
        ListNode aa1 = new ListNode(1);
        ListNode aa2 = new ListNode(2);
        ListNode aa3 = new ListNode(3);
        aa1.next = aa2;
        aa2.next = aa3;
        ListNode listNode = reverseList(aa1);
    }

    /**
     *
     * 示例 1：
     *
     *
     * 输入：head = [1,2,3,4,5]
     * 输出：[5,4,3,2,1]
     * 示例 2：
     *
     *
     * 输入：head = [1,2]
     * 输出：[2,1]
     * 示例 3：
     *
     * 输入：head = []
     * 输出：[]
     */
    public static ListNode reverseList(ListNode head) {
        if (head == null){
            return null;
        }
        ListNode temp = head.next,newRoot = head;
        newRoot.next = null;
        while (temp != null){
            ListNode swap = new ListNode(temp.val);
            swap.next = newRoot;
            newRoot = swap;
            temp = temp.next;
        }
        return newRoot;
    }
}
